package com.biostime.qdingding.modules.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.persistence.TreeList;
import com.biostime.qdingding.common.service.TreeService;
import com.biostime.qdingding.modules.cms.dao.CategoryDao;
import com.biostime.qdingding.modules.cms.entity.Category;

/**
 * 栏目表Service
 * @author king
 * @version 2015-12-11
 */
@Service
@Transactional(readOnly = true)
public class CategoryService extends TreeService<CategoryDao, Category> {
	
	public List<Category> getParentsTree() {
		Category category = new Category();
		List<Category> parents = findList(category);
//		TreeList<Category> treeList = new TreeList<Category>(parents,"├ ");
		
		return parents;
	}
	
	@Override
	public Page<Category> findPage(Page<Category> page, Category entity) {
		page.setOrderBy("a.LAYER,a.sort");
		entity.setPage(page);
		
		TreeList<Category> treeList = new TreeList<Category>(findList(entity),"├ ");
		
		page.setList(treeList.toList());
		return page;
	}
	
	public List<Category> getByParentId(String parentId) {
			
			if(!StringUtils.isEmpty(parentId)){
				Category area = new Category();
				area.setParent(new Category(parentId));
				return findList(area);	
			}
			return new ArrayList<Category>();
		}
	
	@Override
	public List<Category> findList(Category entity) {
		if(entity == null){
			entity = new Category();
		}
		return super.findList(entity);
	}
}
